package com.example.fuzhuang.services;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CaptchaService {

    private static final Logger LOG = LoggerFactory.getLogger(CaptchaService.class);

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 验证码有效期（秒）
    private static final long CAPTCHA_EXPIRATION = 300;

    // 存储验证码
    public void storeCaptcha(String sessionId, String captchaCode) {
        redisTemplate.opsForValue().set(
                "CAPTCHA:" + sessionId,
                captchaCode,
                CAPTCHA_EXPIRATION,
                TimeUnit.SECONDS);
    }

    // 验证验证码
    public boolean validateCaptcha(String sessionId, String userInputCaptcha) {
        String key = "CAPTCHA:" + sessionId;
        String storedCaptcha = redisTemplate.opsForValue().get(key);

        if (storedCaptcha != null && storedCaptcha.equalsIgnoreCase(userInputCaptcha)) {
            // 验证成功后立即删除，防止重复使用
            redisTemplate.delete(key);
            return true;
        }

        return false;
    }
}
